import 'package:flutter/material.dart';
// import 'package:date_format/date_format.dart';

import 'package:date_format/date_format.dart' as dateFormat;
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class DatePage extends StatefulWidget {
  DatePage({Key key}) : super(key: key);
  @override
  _DatePageState createState() => _DatePageState();
}

class _DatePageState extends State<DatePage> {
  DateTime _nowDate = DateTime.now();
  var _nowTime = TimeOfDay(hour: 12, minute: 21);

  _showDatePicker() async {
    // var date = await showDatePicker(
    //   context: context,
    //   initialDate: this._nowDate,
    //   firstDate: DateTime(1900),
    //   // locale: Locale('zh'),
    //   lastDate: DateTime(2100),
    //   // locale: Locale('zh'),
    // );

    // print(date);
    // if (date == null) return;

    // setState(() {
    //   this._nowDate = date;
    // });

    // print(date);

    DatePicker.showDatePicker(
      context,
      onMonthChangeStartWithFirstDate: true,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确认'),
        cancel: Text('取消'),
      ),
      minDateTime: DateTime.parse('2010-05-12'),
      maxDateTime: DateTime.parse('2021-11-25'),
      initialDateTime: _nowDate,
      dateFormat: 'yyyy-MMMM-dd',
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      onChange: (dateTime, List<int> index) {
        // setState(() {
        //   _dateTime = dateTime;
        // });
      },
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _nowDate = dateTime;
        });
      },
    );
  }

  _showTimePicker() async {
    var time = await showTimePicker(
      context: context,
      initialTime: this._nowTime,
    );
    if (time == null) return;

    setState(() {
      this._nowTime = time;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DatePage'),
      ),
      body: Center(
          child: Column(
        children: [
          InkWell(
            onTap: _showDatePicker,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(dateFormat.formatDate(this._nowDate, [
                  dateFormat.yyyy,
                  ' 年 ',
                  dateFormat.mm,
                  ' 月 ',
                  dateFormat.dd,
                  ' 日 '
                ])),
                Icon(
                  Icons.arrow_drop_down,
                  color: Colors.blue,
                )
              ],
            ),
          ),
          InkWell(
            onTap: _showTimePicker,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('${this._nowTime.format(context)}'),
                Icon(
                  Icons.arrow_drop_down,
                  color: Colors.blue,
                )
              ],
            ),
          ),
          InkWell(
            onTap: _showTimePicker,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('使用第三方插件'),
                Icon(
                  Icons.arrow_drop_down,
                  color: Colors.blue,
                )
              ],
            ),
          ),
        ],
      )),
    );
  }
}
